//
//  ParseMessagesXMLReader.m
//  cliqset
//
//  Created by David Hinson on 3/30/09.
//  Copyright 2009 Cliqset, Inc.. All rights reserved.
//

#import "ParseMessagesXMLReader.h"

@implementation ParseMessagesXMLReader

@synthesize contentOfCurrentProperty    = _contentOfCurrentProperty;
@synthesize currentMessageObject        = _currentMessageObject;
@synthesize mode                        = _mode;

NSString *strMessageEnvelope            = @"MessageEnvelope";
NSString *strMessageIdentifier          = @"MessageIdentifier";
NSString *strMessageUserIdentifier      = @"UserIdentifier";
NSString *strGivenNameMess              = @"GivenName";
NSString *strFamilyNameMess             = @"FamilyName";
NSString *strGenderMess                 = @"Gender";
NSString *strAvatarMess                 = @"Avatar";
NSString *strHometownMess               = @"Hometown";
NSString *strSubject                    = @"Subject";
NSString *strTimeSent                   = @"TimeSent";
NSString *strRead                       = @"Read";
NSString *strRecipients                 = @"Recipients";
NSString *strRecipient                  = @"Recipient";
NSString *strBodyMess                   = @"Body";
NSString *strMessage                    = @"Message";

-(id)init {
	[super init];
	
	return self;
}

-(void)resetVars {
	
	self.contentOfCurrentProperty = nil;
	
	if (@selector(_currentMessageObject) && _currentMessageObject!=nil && self.currentMessageObject) {
		[self.currentMessageObject release];
		self.currentMessageObject = nil;
		}
}

- (void)parserDidStartDocument:(NSXMLParser *)parser
{
	[self resetVars];
}

- (void)parseXMLFileAtData:(NSData *)data parseError:(NSError **)error
{	
    NSXMLParser *parser = [[NSXMLParser alloc] initWithData:data];
	
    // Set self as the delegate of the parser so that it will receive the parser delegate methods callbacks.
    [parser setDelegate:self];
	
    // Depending on the XML document you're parsing, you may want to enable these features of NSXMLParser.
    [parser setShouldProcessNamespaces:NO];
    [parser setShouldReportNamespacePrefixes:NO];
    [parser setShouldResolveExternalEntities:NO];
    
    [parser parse];
    
    NSError *parseError = [parser parserError];
    if (parseError && error) {
        *error = parseError;
    }
	
	[self resetVars];
    
    [parser release];
}

- (void)parser:(NSXMLParser *)parser didStartElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName attributes:(NSDictionary *)attributeDict
{
    if (qName) {
        elementName = qName;
    }
	
	if ([elementName isEqualToString:strMessageEnvelope]) {		
		[self resetVars];		
		self.currentMessageObject = [[Message alloc] init];
	} else if ([elementName isEqualToString:strMessageIdentifier]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strMessageUserIdentifier]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strGivenNameMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strFamilyNameMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strGenderMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strAvatarMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strHometownMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strSubject]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strTimeSent]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strRead]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strRecipient]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else if ([elementName isEqualToString:strBodyMess]) {
        self.contentOfCurrentProperty = [NSMutableString string];
	} else {
        self.contentOfCurrentProperty = nil;
	}
}

- (void)parser:(NSXMLParser *)parser didEndElement:(NSString *)elementName namespaceURI:(NSString *)namespaceURI qualifiedName:(NSString *)qName
{     	
    if (qName) {
        elementName = qName;
    }
	
	// Message Identifier
    if ([elementName isEqualToString:strMessageIdentifier]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.messageIdentifier = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.messageIdentifier = (NSMutableString*)@"";
		}
	}
	
	// User Identifier
    if ([elementName isEqualToString:strMessageUserIdentifier]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.userIdentifier = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.userIdentifier = (NSMutableString*)@"";
		}
	}
	
	// Given Name
    if ([elementName isEqualToString:strGivenNameMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.givenName = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.givenName = (NSMutableString*)@"";
		}
	}	
	
	// Family Name
    if ([elementName isEqualToString:strFamilyNameMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.familyName = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.familyName = (NSMutableString*)@"";
		}
	}	
	
	// Gender
    if ([elementName isEqualToString:strGenderMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.gender = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.gender = (NSMutableString*)@"";
		}
	}	
	
	// Avatar
    if ([elementName isEqualToString:strAvatarMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.avatarURL  = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.avatarURL  = (NSMutableString*)@"";
			self.currentMessageObject.sender.avatarBlob = nil;
		}
	}	
	
	// Hometown Locality
    if ([elementName isEqualToString:strHometownMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.sender.hometown = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.sender.hometown = (NSMutableString*)@"";
		}
	}	
	
	// Subject
    if ([elementName isEqualToString:strSubject]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.subject = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.subject = (NSMutableString*)@"";
		}
	}	
	
	// Time Sent
    if ([elementName isEqualToString:strTimeSent]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.timeSent = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.timeSent = (NSMutableString*)@"";
		}
	}	
	
	// Read
    if ([elementName isEqualToString:strRead]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.read = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.read = (NSMutableString*)@"";
		}
	}	
	
	// Body
    if ([elementName isEqualToString:strBodyMess]) {
		if (self.contentOfCurrentProperty) {
			self.currentMessageObject.body = self.contentOfCurrentProperty;
		} else {
			self.currentMessageObject.body = (NSMutableString*)@"Message body is empty.";
		}
	}	
	
	// Recipient
    if ([elementName isEqualToString:strRecipient]) {
		if (self.contentOfCurrentProperty) {
			[self.currentMessageObject.recipients addObject:self.contentOfCurrentProperty];
			// self.currentMessageObject.read = self.contentOfCurrentProperty;
		} else {
			//self.currentMessageObject.read = (NSMutableString*)@"";
		}
	}	
	
	// if ([elementName isEqualToString:strMessageEnvelope] && self.mode == 25 && [self.currentMessageObject.read isEqualToString:@"false"]) {
	if (([elementName isEqualToString:strMessageEnvelope] && self.mode == 25) || ([elementName isEqualToString:strMessageEnvelope] && self.mode == 28)) {
		// we will write the Message to an array here...
		if (self.currentMessageObject) {
			self.currentMessageObject.mode = self.mode;
			[(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addMessage:) withObject:self.currentMessageObject waitUntilDone:YES];
			}
		}
	
	if ([elementName isEqualToString:strBodyMess] && self.mode == 26) {
		// we will write the Message to an array here...
		if (self.currentMessageObject) {
			self.currentMessageObject.mode = self.mode;
			[(id)[[UIApplication sharedApplication] delegate] performSelectorOnMainThread:@selector(addMessage:) withObject:self.currentMessageObject waitUntilDone:YES];
		}
	}
}

- (void)parser:(NSXMLParser *)parser foundCharacters:(NSString *)string
{
    if (self.contentOfCurrentProperty) {
		if (string) {
			string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
			[self.contentOfCurrentProperty appendString:string];
		}
    }
}

-(void) dealloc {
	[_contentOfCurrentProperty release];
	[_currentMessageObject release];	
	[super dealloc];
}

@end
